package com.mangowang.module.main.ui;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.protobuf.InvalidProtocolBufferException;
import com.mangowang.lib.common.base.BaseFragment;
import com.mangowang.lib.common.net.Apis;
import com.mangowang.lib.common.net.NetworkManager;
import com.mangowang.lib.common.net.interfaces.OnRequestCallBack;
import com.mangowang.lib.common.proto.LoginOuterClass;
import com.mangowang.lib.common.proto.Mystock;
import com.mangowang.lib.common.proto.Recharge;
import com.mangowang.lib.common.proto.Transfer;
import com.mangowang.lib.common.util.ValidateUtils;
import com.mangowang.module.main.R;
import com.mangowang.module.main.R2;
import com.mangowang.module.main.adapter.AccountNewAdapter;
import com.mangowang.module.main.bean.AccountBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class AccountNewFragment extends BaseFragment {

    Unbinder unbinder;
    @BindView(R2.id.fragment_recycleView)
    RecyclerView fragmentRecycleView;


    private int page = 1; //
    private int position;  //

    private List<Recharge.My_earnings.wallet_list> datas = new ArrayList<>(); //
    private AccountNewAdapter mAdapter; //
    private int fragmentPosition;

    public static AccountNewFragment NewInstance(int instanceType, String name, int fragmentPosition) {
        Bundle bundle = new Bundle();
        bundle.putInt("position", instanceType);
        bundle.putString("name", name);
        bundle.putInt("fragmentPosition", fragmentPosition);
        AccountNewFragment messageTypeFragment = new AccountNewFragment();
        messageTypeFragment.setArguments(bundle);
        return messageTypeFragment;
    }


    /**
     * 获取布局文件
     *
     * @return
     */
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_recycleview;
    }


    /**
     * 初始化操作步骤
     */
    @Override
    protected void init() {
        unbinder = ButterKnife.bind(this, rootView);
        position = getArguments().getInt("position");
        fragmentPosition = getArguments().getInt("fragmentPosition");
    }

    /**
     * 初始化界面
     */
    @Override
    protected void initView() {
        initRecycle();
        loadData();
    }

    /**
     * 加载数据
     */
    public void loadData() {
        Recharge.My_earnings.Builder builder = Recharge.My_earnings.newBuilder();
        builder.setPattern(String.valueOf(position));
        NetworkManager.INSTANCE.post(Apis.my_earnings, builder.build().toByteArray(), new OnRequestCallBack() {
            @Override
            public void onOk(byte[] response) throws InvalidProtocolBufferException {
                Recharge.My_earnings data = Recharge.My_earnings.parseFrom(response);
                List<Recharge.My_earnings.wallet_list> lists = data.getWalletArrayList();
                mAdapter.addData(lists);
            }

            @Override
            public void onError(int errorCode, String errorMessage) {
//                showToast(errorMessage);
            }
        });


    }


    /**
     * 实例化线性底部RecyclerView
     * *
     */
    private void initRecycle() {
        fragmentRecycleView.setLayoutManager(new LinearLayoutManager(mContext));
        fragmentRecycleView.setAdapter(mAdapter = new AccountNewAdapter(mContext, datas, position));
        mAdapter.addChildClickViewIds(R.id.tv_zhan, R.id.tv_ti_xian);
        mAdapter.setOnItemChildClickListener((adapter, view, position) -> {
                    if (view.getId() == R.id.tv_zhan) {
                        Intent intent = new Intent(mContext, AccountActivity.class);
                        intent.putExtra("title", getArguments().getString("name") + mAdapter.getData().get(position).getWalletName());
                        intent.putExtra("type", mAdapter.getData().get(position).getWalletId());
                        intent.putExtra("balance", mAdapter.getData().get(position).getBalance());
                        intent.putExtra("fragmentPosition", fragmentPosition);
                        startActivity(intent);
                    } else if (view.getId() == R.id.tv_ti_xian) {
                        Intent intent = new Intent(mContext, TiXianActivity.class);
                        intent.setType(mAdapter.getItem(position).getWalletId());
                        intent.putExtra("isCash", mAdapter.getItem(position).getIsCash());
                        intent.putExtra("errorMessage", mAdapter.getItem(position).getErrorCashMsg());
                        if (ValidateUtils.isValidate(mAdapter.getItem(position).getAvailable())) {
                            intent.putExtra("yu_er", mAdapter.getItem(position).getAvailable());
                        } else {
                            intent.putExtra("yu_er", mAdapter.getItem(position).getBalance());
                        }
                        intent.setAction(mAdapter.getItem(position).getWalletName());
                        startActivity(intent);
                    }


                }
        );


    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onResume() {
        super.onResume();
    }
}
